package com.campus.service.impl;

import com.campus.constant.RoomConstant;
import com.campus.mapper.StudentDormSharedMapper;
import com.campus.pojo.dto.StudentDormDTO;
import com.campus.pojo.dto.StudentDormPageQueryDTO;
import com.campus.pojo.vo.StudentDormVO;
import com.campus.result.PageResult;
import com.campus.service.StudentDormSharedService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class StudentDormSharedSharedServiceImpl implements StudentDormSharedService {

    private final StudentDormSharedMapper studentDormSharedMapper;

    /**
     * 学生宿舍信息分页查询
     * @param studentDormPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageStudentDorm(StudentDormPageQueryDTO studentDormPageQueryDTO) {
        PageHelper.startPage(studentDormPageQueryDTO.getPage(), studentDormPageQueryDTO.getPageSize());
        Page<StudentDormVO> page = studentDormSharedMapper.list(studentDormPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加学生宿舍信息
     * @param dormDTO
     */
    @Override
    @Transactional
    public void addStudentDorm(StudentDormDTO dormDTO) {
        //检查该宿舍是否已经满员
        int count = studentDormSharedMapper.getDormPeopleNumber(dormDTO.getDormId());
        if(count >= RoomConstant.MAX_ROOM_PEOPLE){
            throw new RuntimeException("房间已经满员");
        }
        //删除该学生其他宿舍信息
        studentDormSharedMapper.deleteByStudentId(dormDTO.getId());
        //添加学生
        studentDormSharedMapper.addDormInfo(dormDTO);
    }

    /**
     * 根据学生id删除宿舍信息
     * @param studentId
     */
    @Override
    public void deleteStuInfo(Long studentId) {
        studentDormSharedMapper.deleteByStudentId(studentId);
    }

}
